package com.test.controller;


import com.test.common.BaseResponse;
import com.test.common.ErrorCode;
import com.test.common.ResultUtils;
import com.test.exception.BusinessException;
import com.test.model.dto.TFStatDTO;
import com.test.model.dto.UserDTO;
import com.test.model.dto.UserInfoDTO;
import com.test.model.dto.wrongTime.WrongTimeCountsDTO;
import com.test.model.pojo.MultipleQuestion;
import com.test.model.pojo.SingleQuestion;
import com.test.model.pojo.TrueFalseQuestion;
import com.test.model.vo.QuestionPageQueryVO;
import com.test.model.vo.UserVO;
import com.test.service.UserService;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

import static com.test.constant.UserConstant.USER_LOGIN_STATE;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/login")
    public BaseResponse<String> login(@RequestBody UserDTO user, HttpServletRequest request){
        if (StringUtils.isAnyBlank(user.getUsername(), user.getPassword())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        userService.getLogin(user.getUsername(),user.getPassword(),request);
        return ResultUtils.success("登录成功");
    }

    @GetMapping("/exit")
    public BaseResponse<String> exit(HttpServletRequest request){
        userService.getExit(request);
        return ResultUtils.success("退出成功");
    }

    @GetMapping("/getState/{id}")
    public BaseResponse<UserVO> getState(@PathVariable Integer id){
        if(id==null)return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        UserInfoDTO userState = userService.getUserState(id);
        UserVO userVO = new UserVO();
        userVO.setCorrectNums(userState.getCorrectNums());
        userVO.setNickName(userState.getNickname());
        userVO.setSolveNums(userState.getSolveNums());
        userVO.setCorrectRate((userState.getCorrectNums()/userState.getSolveNums())*100);
        userVO.setDefeatRate(100);
        return ResultUtils.success(userVO);
    }

    @GetMapping("/getWrongCount/{type}/{questionId}")
    public BaseResponse<String> setWrongNum(@PathVariable String type,@PathVariable Integer questionId,HttpServletRequest request){
        WrongTimeCountsDTO wrongTimeCountsDTO = new WrongTimeCountsDTO();
        wrongTimeCountsDTO.setType(type);
        wrongTimeCountsDTO.setQuestionId(questionId);
        wrongTimeCountsDTO.setUserId((Integer) request.getSession().getAttribute(USER_LOGIN_STATE));
        userService.updateUserQuestion(wrongTimeCountsDTO,request);
        return null;
    }

    @GetMapping("/getSingleQuestionWrongPage")
    public BaseResponse<QuestionPageQueryVO<SingleQuestion>> getSingleQuestionWrongPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                                                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                                                                        @RequestParam(defaultValue = "") String search
                                                            , HttpServletRequest request){

        // 计算起始行索引
        int startIndex = (pageNum - 1) * pageSize;

        // 使用MyBatis的RowBounds进行分页查询
        RowBounds rowBounds = new RowBounds(startIndex, pageSize);
//        System.out.println(request.getSession().getAttribute(USER_LOGIN_STATE));
        List<SingleQuestion> singleQuestionPage = userService.getSingleQuestionPage(search,rowBounds,request);
        QuestionPageQueryVO<SingleQuestion> questionPageQueryVO = new QuestionPageQueryVO<>(singleQuestionPage, singleQuestionPage.size());
        return ResultUtils.success(questionPageQueryVO);
    }

    @GetMapping("/getMultiQuestionWrongPage")
    public BaseResponse<QuestionPageQueryVO<MultipleQuestion>> getMultiQuestionWrongPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                                                                         @RequestParam(defaultValue = "10") Integer pageSize,
                                                                                         @RequestParam(defaultValue = "") String search
                                                            , HttpServletRequest request){
        // 计算起始行索引
        int startIndex = (pageNum - 1) * pageSize;
        // 使用MyBatis的RowBounds进行分页查询
        RowBounds rowBounds = new RowBounds(startIndex, pageSize);
        List<MultipleQuestion> multipleQuestionPage = userService.getMultiQuestionPage(search,rowBounds,request);
        QuestionPageQueryVO<MultipleQuestion> questionPageQueryVO = new QuestionPageQueryVO<>(multipleQuestionPage, multipleQuestionPage.size());
        return ResultUtils.success(questionPageQueryVO);
    }

    @GetMapping("/getTrueFalseQuestionWrongPage")
    public BaseResponse<QuestionPageQueryVO<TrueFalseQuestion>> getTrueFalseQuestionWrongPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                                                                          @RequestParam(defaultValue = "10") Integer pageSize,
                                                                                          @RequestParam(defaultValue = "") String search
            , HttpServletRequest request){
        // 计算起始行索引
        int startIndex = (pageNum - 1) * pageSize;
        // 使用MyBatis的RowBounds进行分页查询
        RowBounds rowBounds = new RowBounds(startIndex, pageSize);
        List<TrueFalseQuestion> trueFalseQuestionPage = userService.getTrueFalseQuestionPage(search, rowBounds, request);
        QuestionPageQueryVO<TrueFalseQuestion> questionPageQueryVO = new QuestionPageQueryVO<>(trueFalseQuestionPage, trueFalseQuestionPage.size());
        return ResultUtils.success(questionPageQueryVO);
    }

}
